﻿@using Grand.Domain.Catalog
@model ProductListModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = T("Admin.Catalog.Products.Manage").Text;
}
<form asp-area="@Constants.AreaAdmin" asp-controller="Product" asp-action="List" method="post">
    
    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-cubes"></i>
                        @T("Admin.Catalog.Products.Manage")
                    </div>
                    <div class="actions btn-group btn-group-devided util-btn-margin-bottom-5">
                        <a href="@Url.Action("Create")" class="btn green"><i class="fa fa-plus"></i><span class="d-none d-sm-inline"> @T("Admin.Common.AddNew") </span></a>
                        <button type="submit" name="download-catalog-pdf" class="btn purple">
                            <i class="fa fa-file-pdf-o"></i>
                            @T("Admin.Catalog.Products.List.DownloadPDF")
                        </button>
                        <div class="btn-group btn-group-devided">
                            <a class="btn default" href="javascript:;" data-toggle="dropdown" data-hover="dropdown" data-close-others="true">
                                <i class="fa fa-download"></i>
                                @T("Admin.Common.Export")
                                <i class="fa fa-angle-down"></i>
                            </a>
                            <ul class="dropdown-menu pull-right">
                                <li>
                                    <input type="submit" name="exportxml-all" class="btn default" style="width: 100%;" value="@T("Admin.Common.ExportToXml.All")" />
                                </li>
                                <li>
                                    <input type="button" id="exportxml-selected" class="btn default" style="width: 100%;" value="@T("Admin.Common.ExportToXml.Selected")" />
                                </li>
                                <li>
                                    <input type="submit" name="exportexcel-all" class="btn default" style="width: 100%;" value="@T("Admin.Common.ExportToExcel.All")" />
                                </li>
                                <li>
                                    <input type="button" id="exportexcel-selected" class="btn default" style="width: 100%;" value="@T("Admin.Common.ExportToExcel.Selected")" />
                                </li>
                            </ul>
                        </div>
                        @if (!Model.IsLoggedInAsVendor)
                        {
                            //a vendor cannot import products
                            <button type="button" id="importexcel" name="importexcel" class="btn default">
                                <i class="fa fa-upload"></i>
                                @T("Admin.Common.ImportFromExcel")
                            </button>

                        }
                        <button type="button" name="delete-selected" id="delete-selected" class="btn red">
                            <i class="fa fa-trash-o"></i>
                            @T("Admin.Common.Delete.Selected")
                        </button>
                        <vc:admin-widget widget-zone="product_list_buttons" additional-data="null" />
                    </div>
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body col-md-6 advanced-filter">
                            <div class="form-group">
                                <admin-label asp-for="SearchProductName" class="control-label col-3 col-md-4 col-sm-4" />
                                <div class="col-9 col-md-8 col-sm-8">
                                    <admin-input asp-for="SearchProductName" />
                                </div>
                            </div>
                        </div>
                        <div class="form-body col-md-6 advanced-filter">
                            <div class="form-group">
                                <div class="col-12 col-md-12 col-sm-12">
                                    <div id="ExtendFilter">
                                        <button class="btn btn-success filter-submit" id="search-products">
                                            <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                        </button>
                                        <button type="button" class="btn btn-default filter-extend-button">
                                            <i class="fa fa-filter"></i> <span class="filter-text-close">@T("Admin.Common.Search.Filter.Open")</span>
                                            <span class="filter-text-open">@T("Admin.Common.Search.Filter.Close")</span>
                                        </button>
                                    </div>
                                </div>

                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="form-body col-sm-6 extend-collapse">
                            <div class="form-group">
                                <admin-label asp-for="SearchProductTypeId" class="control-label col-4 col-md-4 col-sm-4" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="SearchProductTypeId" asp-items="Model.AvailableProductTypes" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchPublishedId" class="control-label col-4 col-md-4 col-sm-4" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="SearchPublishedId" asp-items="Model.AvailablePublishedOptions" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchWarehouseId" class="control-label col-4 col-md-4 col-sm-4" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="SearchWarehouseId" asp-items="Model.AvailableWarehouses" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="GoDirectlyToSku" class="control-label col-4 col-md-4 col-sm-4" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <div class="input-group" style="text-align:left">
                                        <admin-input asp-for="GoDirectlyToSku" />
                                        <span class="input-group-append">
                                            <input type="submit" id="go-to-product-by-sku" name="go-to-product-by-sku" class="btn default" value="@T("Admin.Common.Go")" />
                                        </span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-body col-sm-6 extend-collapse">
                            <div class="form-group">
                                <admin-label asp-for="SearchCategoryId" class="control-label col-4 col-md-4 col-sm-4" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="SearchCategoryId" asp-items="Model.AvailableCategories" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchIncludeSubCategories" class="control-label col-4 col-md-4 col-sm-4" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <label class="mt-checkbox mt-checkbox-outline control control-checkbox">
                                        <admin-input asp-for="SearchIncludeSubCategories" />
                                         <div class="control__indicator"></div>
                                    </label>
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchManufacturerId" class="control-label col-4 col-md-4 col-sm-4" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="SearchManufacturerId" asp-items="Model.AvailableManufacturers" />
                                </div>
                            </div>
                            <div class="form-group">
                                <admin-label asp-for="SearchStoreId" class="control-label col-4 col-md-4 col-sm-4" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                </div>
                            </div>
                            <div class="form-group" @(Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                <admin-label asp-for="SearchVendorId" class="control-label col-4 col-md-4 col-sm-4" />
                                <div class="col-8 col-md-8 col-sm-8">
                                    <admin-select asp-for="SearchVendorId" asp-items="Model.AvailableVendors" />
                                </div>
                            </div>
                        </div>
                        <div class="clearfix"></div>
                        <div class="x_content">
                            <div id="products-grid"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(document).ready(function () {
            $('.extend-collapse').hide();
            $('.filter-text-open').hide();
            $('.filter-extend-button').click(function () {
                $('.extend-collapse').toggle("slow");
                $('.filter-text-open').toggle();
                $('.filter-text-close').toggle();
            });
            
            $("#products-grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("ProductList", "Product"))",
                            type: "POST",
                            dataType: "json",
                            data: additionalData
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(adminAreaSettings.DefaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true,
                    requestStart: function () {
                        kendo.ui.progress($('#products-grid'), true);
                    },
                    requestEnd: function () {
                        kendo.ui.progress($('#products-grid'), false);

                    },
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(adminAreaSettings.GridPageSizes)]
                },
                editable: {
                    confirmation: false,
                    mode: "inline"
                },
                scrollable: false,
                dataBound: onDataBound,
                columns: [
                    {
                        field: "Id",
                        headerTemplate: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox mt-NoMargin'><input id='mastercheckbox' type='checkbox'/><div class='control__indicator control__indicator_custom mt-top2 rtlabel'></div></label>",
                        template: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox mt-NoMargin'><input type='checkbox' value='#=Id#' class='checkboxGroups'/><div class='control__indicator control__indicator_custom mt-top2'></div></label>",
                        width: 50
                    },
                    {
                        field: "PictureThumbnailUrl",
                        title: "@T("Admin.Catalog.Products.Fields.PictureThumbnailUrl")",
                        template: '<a class="k-link" href="Edit/#=Id#"><img src="#=PictureThumbnailUrl#" /></a>',
                        width: 92
                    },
                    {
                        field: "Name",
                        title: "@T("Admin.Catalog.Products.Fields.Name")",
                        encoded: false,
                        template: '<a class="k-link" href="Edit/#=Id#">#=kendo.htmlEncode(Name)#</a>',
                        width: 270
                    }, {
                        field: "Sku",
                        title: "@T("Admin.Catalog.Products.Fields.Sku")",
						headerAttributes: { style: "text-align:center" },
						attributes: { style: "text-align:center" },
                        width: 80
                    }, {
                        field: "Price",
                        title: "@T("Admin.Catalog.Products.Fields.Price")",
						headerAttributes: { style: "text-align:right" },
						attributes: { style: "text-align:right" },
                        width: 100,
                        //if not a grouped product, then display
                        template: "# if(ProductTypeId != @((int)ProductType.GroupedProduct)) {# #:Price# #}  #"
                    }, {
                        field: "StockQuantityStr",
                        title: "@T("Admin.Catalog.Products.Fields.StockQuantity")",
						headerAttributes: { style: "text-align:right" },
						attributes: { style: "text-align:right" },
						minScreenWidth: 750,
                        width: 80
                    }, {
                        field: "ProductTypeName",
                        title: "@T("Admin.Catalog.Products.Fields.ProductType")",
						headerAttributes: { style: "text-align:center" },
						attributes: { style: "text-align:center" },
                        width: 100,
						minScreenWidth: 1050
                    }, {
                        field: "Published",
                        title: "@T("Admin.Catalog.Products.Fields.Published")",
                        width: 85,
                        headerAttributes: { style: "text-align:center" },
                        attributes: { style: "text-align:center" },
                        template: '# if(Published) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
                    }
                ]
            });

        });
    </script>

    <script>

        var selectedIds = [];

        $(document).ready(function () {

            //search button
            $('#search-products').click(function () {
                //search
                var grid = $('#products-grid').data('kendoGrid');
                grid.dataSource.page(1); //new search. Set page size to 1
                //grid.dataSource.read(); we already loaded the grid above using "page" function
                //clear selected checkboxes
                $('.checkboxGroups').prop('checked', false).change();
                selectedIds = [];
                return false;
            });

            $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#search-products").click();
                    return false;
                }
            });
            $("#@Html.FieldIdFor(model => model.GoDirectlyToSku)").keydown(function (event) {
                if (event.keyCode == 13) {
                    $("#go-to-product-by-sku").click();
                    return false;
                }
            });

            //delete selected
            $('#delete-selected').click(function(e) {

                e.preventDefault();

                var postData = {
                    selectedIds: selectedIds
                };
                addAntiForgeryToken(postData);

                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@(Url.Action("DeleteSelected", "Product"))",
                    data: postData,
                    complete: function(data) {
                        //reload grid
                        var grid = $('#products-grid').data('kendoGrid');
                        grid.dataSource.read();
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError);
                    },
                    traditional: true
                });
                return false;
            });

            $('#mastercheckbox').click(function () {
                $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
            });

            //wire up checkboxes.
            $('#products-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
                var $check = $(this);
                if ($check.is(":checked") == true) {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked == -1) {
                        //add id to selectedIds.
                        selectedIds.push($check.val());
                    }
                }
                else {
                    var checked = jQuery.inArray($check.val(), selectedIds);
                    if (checked > -1) {
                        //remove id from selectedIds.
                        selectedIds = $.grep(selectedIds, function (item, index) {
                            return item != $check.val();
                        });
                    }
                }
                updateMasterCheckbox();
            });
        });

        function additionalData() {
            var data = {
                SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
                SearchCategoryId: $('#SearchCategoryId').val(),
                SearchIncludeSubCategories: $('#@Html.FieldIdFor(model => model.SearchIncludeSubCategories)').is(':checked'),
                SearchManufacturerId: $('#SearchManufacturerId').val(),
                SearchStoreId: $('#SearchStoreId').val(),
                SearchWarehouseId: $('#SearchWarehouseId').val(),
                SearchVendorId: $('#SearchVendorId').val(),
                SearchProductTypeId: $('#SearchProductTypeId').val(),
                SearchPublishedId: $('#SearchPublishedId').val()
            };
            addAntiForgeryToken(data);
            return data;
        }

        function onDataBound(e) {
            $('#products-grid input[type=checkbox][id!=mastercheckbox]').each(function () {
                var currentId = $(this).val();
                var checked = jQuery.inArray(currentId, selectedIds);
                //set checked based on if current checkbox's value is in selectedIds.
                $(this).prop('checked', checked > -1);
            });

            updateMasterCheckbox();
        }

        function updateMasterCheckbox() {
            var numChkBoxes = $('#products-grid input[type=checkbox][id!=mastercheckbox]').length;
            var numChkBoxesChecked = $('#products-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
            $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
        }
    </script>
</form>

@*import products form*@
<div class="modal-dialog" id="importexcel-window" style="display:none; padding: 0 10px;">
    <form asp-area="@Constants.AreaAdmin" asp-controller="Product" asp-action="ImportExcel" method="post" enctype="multipart/form-data">
        
        <em>@T("Admin.Catalog.Products.List.ImportFromExcelTip")</em>
        <div class="form-group">
            &nbsp;
        </div>
        <div class="form-group">
            <div class="col-md-2" style="margin-top: 2px; height: 34px; padding-left: 25px;">
                <label class="control-label">
                    @T("Admin.Common.ExcelFile")
                </label>
            </div>
            <div class="col-md-10">
                <input type="file" id="importexcelfile" name="importexcelfile" />
            </div>
        </div>
        <div class="form-group">
            &nbsp;
        </div>
        <div class="form-group">
            <div class="col-md-2">
                &nbsp;
            </div>
            <div class="col-md-10 text-right mt-10">
                <input type="submit" class="k-button" value="@T("Admin.Common.ImportFromExcel")" />
            </div>
        </div>
    </form>
</div>
<script>
        $(document).ready(function () {
            $("#importexcel").click(function (e) {
                e.preventDefault();
                var window = $("#importexcel-window");
                if (!window.data("kendoWindow")) {
                    window.kendoWindow({
                        modal: true,
                        title: "@T("Admin.Common.ImportFromExcel")",
                        actions: ["Close"]
                    });
                }
                window.data('kendoWindow').center().open();
            });
        });
</script>
@*export selected (XML). We don't use GET approach because it's limited to 2K-4K chars and won't work for large number of entities*@
<form asp-area="@Constants.AreaAdmin" asp-controller="Product" asp-action="ExportXmlSelected" method="post" id="export-xml-selected-form">
    
    <input type="hidden" id="selectedIds" name="selectedIds" value="" />
</form>

<script>

    $(document).ready(function () {
        $('#exportxml-selected').click(function (e) {
            e.preventDefault();
            var ids = selectedIds.join(",");
            $('#export-xml-selected-form #selectedIds').val(ids);
            $('#export-xml-selected-form').submit();
            return false;
        });
    });

</script>
@*export selected (Excel). We don't use GET approach because it's limited to 2K-4K chars and won't work for large number of entities*@
<form asp-area="@Constants.AreaAdmin" asp-controller="Product" asp-action="ExportExcelSelected" method="post" id="export-excel-selected-form">
    
    <input type="hidden" id="selectedIds" name="selectedIds" value="" />
</form>
<script>
    $(document).ready(function () {
        $('#exportexcel-selected').click(function (e) {
            e.preventDefault();
            var ids = selectedIds.join(",");
            $('#export-excel-selected-form #selectedIds').val(ids);
            $('#export-excel-selected-form').submit();
            return false;
        });
    });
</script>
